<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/5/12
 * Time: 2:18 PM
 * To change this template use File | Settings | File Templates.
 */
class Db_Payments_PaymentMethodsFields extends  Common {

	protected static $tableName = TAB_PAYMENT_METHODS_FIELDS;
	protected static $dbFields = array(
		'id',
		'pmf_field_name',
		'pmf_field_type',
		'pmf_payment_method_id',
		'deleted'
	);
	/**
	 * @var Db_Payments_PaymentMethodsFields
	 */
	public $objectArray;

	public $id;
	public $pmf_field_name;
	public $pmf_field_type;
	public $pmf_payment_method_id;
	public $deleted;

	function __construct($value = null, $fieldName = null) {
		parent::__construct($value, $fieldName);
	}

	public static function getAllPaymentMethodsFields($limit, $sqlSearch = '', $orderBy = '') {
		$joinSearch = null;
		if (empty($sqlSearch)) {
			$sqlSearch = '1=1';
			$joinSearch = '';
		} else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pm.pm_module AND tr.t_key = pm.pm_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlSearch .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}

		if(empty($orderBy)) {
			$orderBy = ' ORDER BY `pmf`.`id`';
		}
		$sql = "SELECT *, `pmf`.`id` AS `pmf_id` FROM ?f AS `pmf`
				INNER JOIN ?f AS `pm` ON `pmf`.`pmf_payment_method_id` = `pm`.`id`
				{$joinSearch}
				WHERE `pmf`.`deleted` = 0 AND `pm`.`deleted` = 0
				AND {$sqlSearch}
				{$orderBy} {$limit}";
		return self::$DB->select($sql, self::$tableName, TAB_PAYMENT_METHODS);
	}

	public static function getTotalPaymentMethodsFields($sqlCondition = '1=1') {
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
			$joinSearch = '';
		} else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pm.pm_module AND tr.t_key = pm.pm_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlCondition .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}

		$sql = "SELECT COUNT(*) FROM ?f AS `pmf`
				INNER JOIN ?f AS `pm` ON `pmf`.`pmf_payment_method_id` = `pm`.`id`
				{$joinSearch}
				WHERE `pmf`.`deleted` = 0 AND `pm`.`deleted` = 0
				AND {$sqlCondition}
				";
		return self::$DB->selectCell($sql, self::$tableName, TAB_PAYMENT_METHODS);
	}


}
